package cn.tedu.csmall.passportbackup.web;

import cn.tedu.csmall.passportbackup.ex.ServiceException;
import lombok.Data;

@Data
public class JsonResult<T> {
    /**
     * 业务状态码
     */
    private Integer state;
    /**
     * 错误时的提示消息
     */
    private String message;
    /**
     * 成功时的数据
     */
    private T data;


    public static JsonResult ok(){
        return JsonResult.ok(null);
    }

    public static <T> JsonResult<T> ok(T data){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(data);
        jsonResult.setState(ServiceCode.OK);
        return jsonResult;
    }


    public static JsonResult<Void> fail(ServiceException e){
        return JsonResult.fail(e.getState(),e.getMessage());
    }

    public static JsonResult<Void> fail(Integer state,String message){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setState(state);
        jsonResult.setMessage(message);
        return jsonResult;
    }
}
